﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Interface.Models;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CNKI.TPI.Web.Interface.Areas.Category.Controllers
{
    public class GetCategoryNodeController : BaseApiController
    {
        INaviService naviService = null;
        public GetCategoryNodeController(INaviService naviService)
        {
            this.naviService = generator.CreateInterfaceProxyWithTarget<INaviService>(naviService, new IInterceptor[] { new LogInterceptor() });
        }

        // POST api/getcategorynode
        public void Post([FromBody]string value)
        {
        }

        /// <summary>
        /// 根据数据库名称，数据库id，分类导航ID获取节点数据
        /// 当父节点不为空时，根据父节点ID返回所有子节点
        /// </summary>
        /// <param name="dbCode">数据库编码Code</param>
        /// <param name="dbID">数据库ID</param>
        /// <param name="catNavID">分类导航ID</param>
        /// <param name="catCode">分类code 父节点ID</param>
        /// <returns></returns>
        public object Get(string dbcode, int dbid, int cattypeid, string catcode)
        {
            OutputEntity result = new OutputEntity();
            try
            {
                IList<CLS_NODE> categoryList;
                if (String.IsNullOrEmpty(catcode))
                {
                    categoryList = naviService.GetCategoryList(dbcode, cattypeid, dbid);
                }
                else
                {
                    categoryList = naviService.GetChildrenCategoryList(dbcode, cattypeid, catcode, dbid);
                }
                if (categoryList != null)
                {
                    result.Status = Status.Success;
                    result.data = categoryList;
                }
                else
                {
                    result.Status = Status.Error;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
                result.Status = Status.Error;
                result.message = ex.Message;
            }
            return result;

        }
    }
}
